<?php
namespace app\admin\controller;
use think\Controller;
use Common\Controller\BaseController;
use think\Request;
use think\Session;
class Admin extends Controller
{
    public function index(){
        return $this->fetch();
    }
    public function admin(){
        $user = session('user');
        //dump($user);exit;
        if(!$user){
            header("Refresh:1;url=login");
        }
        $res=db('admin')->paginate(10);
        $page =  $res->render();
        $this->assign('page',$page);
        $this->assign('list',$res);
        return $this->fetch();
    }
//管理员添加人员开始
    public function admin_insert(){
        $user = session('user');
        //dump($user);exit;
        if(!$user){
            header("Refresh:1;url=login");
        }
        $res=db('admin')->select();

        $this->assign('list',$res);
        return $this->fetch();
    }
    public function adminadd(){
        $user = session('user');
        //dump($user);exit;
        if(!$user){
            header("Refresh:1;url=login");
        }
        if(!$_POST) {
            echo "<script>alert('提交不能为空');location.href='admin_insert';</script>";
        }
        $na['name'] = trim($_POST['name']);
        $res = db('admin')->where($na)->find();
        if($na['name'] == $res['name']){
            //return ['code'=>2,'msg'=>'添加账号重复'];
            echo "<script>alert('添加账号重复');location.href='admin_insert';</script>";
        }
        //dump($_POST);exit;
        $data['name'] = trim($_POST['name']);
        $data['password'] = md5(md5(trim($_POST['password'])));
        $data['jurisdiction'] = trim($_POST['jurisdiction']);
        $data['operation'] = session('user.name');
        $data['time'] = time();
        //dump($data);exit;
        $res=db('admin')->insert($data);
        if($res){
            //return ['code'=>1,'msg'=>'添加信息成功'];
            echo "<script>alert('添加成功');location.href='admin';</script>";
        }else{
            //return ['code'=>3,'msg'=>'添加信息失败'];
            echo "<script>alert('添加失败');location.href='admin';</script>";
        }
    }
//管理员添加管理人员结束
//管理员编辑管理人员开始
    public function admin_det(){
        $user = session('user');
        //dump($user);exit;
        if(!$user){
            header("Refresh:1;url=login");
        }
        if(!$_GET) {
            echo "<script>alert('提交不能为空');location.href='admin_det';</script>";
            //$this->success('提交不能为空', 'express');
        }
        $ress=db('admin')->select();
        $id['id']=trim($_GET['id']);
        $res=db('admin')->where($id)->find();
        //dump($res);exit;
        $this->assign('listt',$ress);
        //dump($ress);exit;
        $this->assign('list',$res);
        return $this->fetch();
    }
    public function adminupdata(){
        $user = session('user');
        //dump($user);exit;
        if(!$user){
            header("Refresh:1;url=login");
        }
        if(!$_POST) {
            echo "<script>alert('提交不能为空');location.href='admin';</script>";
            //$this->success('提交不能为空', 'express');
        }
        $id['id'] = trim($_GET['id']);
        //dump($_POST);exit;
        //$data = $_POST;
        $data['name'] = trim(input('name'));
        if(!trim($_POST['password']) == "") {
            $data['password'] = md5(md5(trim(input('password'))));
        }
        $data['jurisdiction'] = trim(input('jurisdiction'));
        $data['operation'] = session('user.name');
        $data['time'] = time();
        //dump($data);exit;
        $res=db('admin')->where($id)->update($data);
        if($res){
            echo "<script>alert('更新信息成功');location.href='admin';</script>";
            //return ['code'=>1,'msg'=>'更新信息成功'];
        }else{
            echo "<script>alert('更新信息失败');location.href='admin';</script>";
            //return ['code'=>0,'msg'=>'更新信息失败'];
        }
    }
//管理员编辑管理人员结束
//管理员删除管理人员开始
    public function admindelect(){
        $user = session('user');
        //dump($user);exit;
        if(!$user){
            header("Refresh:1;url=login");
        }
        if(!$_GET){
            echo "<script>alert('提交不能为空');location.href='admin';</script>";
            //$this->success('提交不能为空','admin');
        }
        $data['id'] = trim($_GET['id']);
        $res=db('admin')->where($data)->delete();
        if($res){
            echo "<script>alert('删除成功');javascript:history.go(-1);</script>";
            //return ['code'=>1,'msg'=>'删除信息成功'];
        }else{
            echo "<script>alert('删除失败');javascript:history.go(-1);</script>";
            //return ['code'=>0,'msg'=>'删除信息失败'];
        }
    }
//管理员删除管理人员结束
}